<?php

include_once dirname(dirname(__FILE__)) . '/include/common.php';
$core = new core();
$payment_id = isset($_POST["payment_id"]) ? intval($_POST["payment_id"]) : 0;
$message = '';
$customer_order_id = 0;
if ($payment_id > 0) {
    $data_payment = $core->getPaymentById($payment_id);
    if ($data_payment) {
        $customer_order = $core->getZenCustomerOrderById($data_payment->customer_order_id);
        $customer_order_id = $data_payment->customer_order_id;
        //update total_payment,total_current_money
        $total_payment = $customer_order->total_payment - $data_payment->payment;
        $total_payment = core::roundingMoney($total_payment);
        $total_current_money = $customer_order->total_current_money - $data_payment->payment;
        $total_current_money = core::roundingMoney($total_current_money);

        $core->db->query("UPDATE zen_customer_order SET total_payment=$total_payment,
                        total_current_money= $total_current_money
                            WHERE customer_order_id=$customer_order_id");

        $core->db->query("DELETE FROM zen_payment  WHERE id=$payment_id");

        $message = "Xóa payment thành công!";
    } else {
        echo json_encode(array('type' => 1, 'msg' => "payment không tồn tại!"));
        die;
    }
} else {
    echo json_encode(array('type' => 1, 'msg' => "payment không tồn tại!"));
    die;
}
$html = $core->renderPayment($customer_order_id);
echo json_encode(array('type' => 0, 'msg' => $message, 'html' => $html));
die;
?>